package com.wyn.common.controller;

import com.wyn.common.domain.User;
import com.wyn.common.exception.ControllerException;
import com.wyn.common.service.UserService;
import com.wyn.common.utils.JWTUtils;
import com.wyn.common.utils.R;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/User")
@Api(tags = {"测试管理控制器"})
public class DemoController extends BaseController{


    @Autowired
    UserService userService;

    @PostMapping("/login")
    public R<Map> loginUser(@RequestBody User user) {
        //数据库查找到当前用户
        User u = userService.getOne(user);
        //如果查询到数据就登陆成功否则失败
        if (u != null){
            //将需要token存储的信息放入map集合
            Map<String, String> key = new HashMap<>();
            key.put("password", u.getPassWord());
            //通过工具类生成token
            String token = JWTUtils.getToken(key);
            //返回一个当前用户的信息和token的map集合,然后交给前端人员，设置访问时将token设置到请求头里面
            Map<String, Object> v = new HashMap<>();
            v.put("userId", u.getId());
            v.put("username", u.getUserName());
            v.put("token", token);
            return new R<>(OK,OKMsg,v);
        }else {
            throw new ControllerException(NOMsg);
        }

    }

    @PostMapping("/list")
    public R<List<User>> getList(Integer id){
        return new R<>(OK,OKMsg);
    }
}
